﻿using Pdoxcl2Sharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using V3MainTool.Model;

namespace V3MainTool.UI
{
    /// <summary>
    /// UCProductionMethod.xaml 的交互逻辑
    /// </summary>
    public partial class UCProductionMethod : UserControl
    {
        string fold;
        List<ProductionMethodFamily> list = new List<ProductionMethodFamily>();
        List<string> fileList = new List<string>();
        public UCProductionMethod()
        {
            InitializeComponent();
            cmbType.Items.Add("工业"); fileList.Add("01_industry.txt");
            cmbType.Items.Add("农业"); fileList.Add("02_agro.txt");
            cmbType.Items.Add("矿井"); fileList.Add("03_mines.txt");
            cmbType.Items.Add("种植园"); fileList.Add("04_plantations.txt");
            cmbType.Items.Add("军事"); fileList.Add("05_military.txt");
            cmbType.Items.Add("城镇中心"); fileList.Add("06_urban_center.txt");
            cmbType.Items.Add("政府"); fileList.Add("07_government.txt");
            cmbType.Items.Add("奇观"); fileList.Add("08_monuments.txt");
            cmbType.Items.Add("杂项资源"); fileList.Add("09_misc_resource.txt");
            cmbType.Items.Add("运河"); fileList.Add("10_canals.txt");
            cmbType.Items.Add("基础设施"); fileList.Add("11_private_infrastructure.txt");
            cmbType.Items.Add("自耕"); fileList.Add("12_subsistence.txt");
            cmbType.Items.Add("土建"); fileList.Add("13_construction.txt");
            cmbType.SelectedIndex = 0;
            cmbType.SelectionChanged += CmbType_SelectionChanged;
        }

        private void btnReload_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                fold = MainWindow.Instance.GetFold();
                string mypath = fold + @"\common\production_methods\";
                List<string> paths = new List<string>();
                paths.Add(fold + @"\localization\simp_chinese\production_methods_l_simp_chinese.yml");
                paths.Add(fold + @"\localization\simp_chinese\production_methods_rufus_l_simp_chinese.yml");
                LocalHelper.ProductionMethod = LocalHelper.GetDic(paths);
                LocalHelper.InventionsDic = LocalHelper.GetDic(fold + @"\localization\simp_chinese\inventions_l_simp_chinese.yml");
                for (int i = 0; i < fileList.Count; i++)
                {
                    using (FileStream fs = new FileStream(mypath + fileList[i], FileMode.Open))
                    {
                        this.list.Add(ParadoxParser.Parse(fs, new ProductionMethodFamily()));
                    }
                }
                LoadSelectedList();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
        private void CmbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadSelectedList();
        }
        private void LoadSelectedList()
        {
            if (cmbType.SelectedIndex >= list.Count)
            {
                dgv.ItemsSource = null;
                return;
            }
            dgv.ItemsSource = list[cmbType.SelectedIndex].MyList;
        }
        private void btnOpenFold_Click(object sender, RoutedEventArgs e)
        {
            fold = MainWindow.Instance.GetFold();
            System.Diagnostics.Process.Start("explorer.exe", fold + @"\common\production_methods\");
        }
    }
}
